package com.simple.vo;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.simple.exp.EndExcelException;
import com.simple.mapper.nrmp.KnowledgePointMapper;
import com.simple.question.KnowledgePoint;
import com.simple.util.ExcelUtil;
import com.simple.util.SpringBeanUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AFanTi {
	
	private static Map<String, Long> map = new HashMap<>();
	
	private static Map<String, String> kpointChangeMap = new HashMap<>();
	
	private static Map<String, KnowledgePoint> kpointMap = null;
	
	private static Object synObject = new Object();
	
	static {
		map.put("语文", 1L);
		map.put("数学", 2L);
		map.put("英语", 3L);
		map.put("政治", 4L);
		map.put("历史", 5L);
		map.put("地理", 6L);
		map.put("生物", 7L);
		map.put("物理", 8L);
		map.put("化学", 9L);
		map.put("科学", 10L);
		loadFile("初中-化学-阿凡题知识点.xlsx");
		loadFile("初中-数学-阿凡题知识点.xlsx");
		loadFile("初中-物理-阿凡题知识点.xlsx");
		loadFile("初中-英语-阿凡题知识点.xlsx");
		loadFile("高中-化学-阿凡题知识点.xlsx");
		loadFile("高中-数学-阿凡题知识点.xlsx");
		loadFile("高中-物理-阿凡题知识点.xlsx");
		loadFile("小学-数学-阿凡题知识点.xlsx");
	}
	
	private String trunkHtml; //题干Html
	private String analysHtml; //解析Html //可能为空
	private String answerHtml; //答案Html
	private long subjectId = 0; //科目id  //如果为0表示未知
	private long phaseId = 0; //学段id //如果为0表示未知
	private String subjectName; //科目名称  //如果为空表示未知
	private String knowledgePointName; //知识点名称（非我们数据库的知识点）
	private String knowledgePointId; //知识点id
	private long questionId;
	private String origin; //阿凡题接口原始信息
	private float searchScore = 0; //匹配率
	
	public String getTrunkHtml() {
		return trunkHtml;
	}
	public void setTrunkHtml(String trunkHtml) {
		if(trunkHtml != null){		
			trunkHtml = trunkHtml.replace("//statistics", " ");
			trunkHtml = trunkHtml.replace("//insert video data", " ");
			trunkHtml = trunkHtml.replace("//init video slide", " ");
			this.trunkHtml = trunkHtml.replace("\n", "");
		}
	}
	public String getAnalysHtml() {
		return analysHtml;
	}
	public void setAnalysHtml(String analysHtml) {
		if(analysHtml != null){			
			analysHtml = analysHtml.replace("//statistics", " ");
			analysHtml = analysHtml.replace("//insert video data", " ");
			analysHtml = analysHtml.replace("//init video slide", " ");
			this.analysHtml = analysHtml.replace("\n", "");
		}
	}
	public String getAnswerHtml() {
		return answerHtml;
	}
	public void setAnswerHtml(String answerHtml) {
		if(answerHtml != null){		
			answerHtml = answerHtml.replace("//statistics", " ");
			answerHtml = answerHtml.replace("//insert video data", " ");
			answerHtml = answerHtml.replace("//init video slide", " ");
			this.answerHtml = answerHtml.replace("\n", "");
		}
	}
	public String getSubjectName() {
		return subjectName;
	}
	public void setSubjectName(String subjectName) {
		if(subjectName != null && (subjectName.startsWith("初中") || subjectName.startsWith("高中") || subjectName.startsWith("小学"))) {
			if(subjectName.startsWith("小学")) {
				phaseId = 2;
			}
			if(subjectName.startsWith("初中")) {
				phaseId = 3;
			}
			if(subjectName.startsWith("高中")) {
				phaseId = 4;
			}
			subjectName = subjectName.substring(2);
		}
		if(map.containsKey(subjectName)) {
			this.subjectName = subjectName;
			this.subjectId = map.get(subjectName);
		}
		else {
			this.subjectName = null;
		}
	}
	public String getKnowledgePointName() {
		return knowledgePointName;
	}
	public void setKnowledgePointName(String knowledgePointName) {
		this.knowledgePointName = knowledgePointName;
	}
	public long getSubjectId() {
		return subjectId;
	}
	public long getQuestionId() {
		return questionId;
	}
	public void setQuestionId(long questionId) {
		this.questionId = questionId;
	}
	public String getKnowledgePointId() {
		return knowledgePointId;
	}
	public void setKnowledgePointId(String knowledgePointId) {
		this.knowledgePointId = knowledgePointId;
	}
	public void setSubjectId(long subjectId) {
		this.subjectId = subjectId;
	}
	public long getPhaseId() {
		return phaseId;
	}
	public void setPhaseId(long phaseId) {
		this.phaseId = phaseId;
	}
	public String getOrigin() {
		return origin;
	}
	public void setOrigin(String origin) {
		this.origin = origin;
	}
	
	public void parseKnowledge(JSONArray knowledgeArray) {
		for(int i = 0; i < knowledgeArray.size(); i++) {
			JSONObject knowledgeObject = knowledgeArray.getJSONObject(i);
			long id = knowledgeObject.getLongValue("id");
			String name = knowledgeObject.getString("name");
			List<KnowledgePoint> kpoints = getKpoints(id, name);
			if(!kpoints.isEmpty()) {
				String knowledgePointId = "";
				String knowledgePointName = "";
				for(KnowledgePoint knowledgePoint: kpoints) {
					knowledgePointId += knowledgePoint.getId() + ";";
					knowledgePointName += knowledgePoint.getName() + ";";
				}
				this.knowledgePointId = knowledgePointId.substring(0, knowledgePointId.length() - 1);
				this.knowledgePointName = knowledgePointName.substring(0, knowledgePointName.length() - 1);
			}
		}
	}
	
	private static void loadFile(String path) {
		ExcelUtil excelUtil = new ExcelUtil();
		excelUtil.setExcelRowHandle(new ExcelUtil.ExcelRowHandle() {
			
			@Override
			public void onSheetEnd(int sheetIndex, String sheetName) {
				throw new EndExcelException();
			}
			
			@Override
			public void onRow(int sheetIndex, String sheetName, int curRow, List<String> rowList) {
				if(rowList.size()==0||rowList.get(0).trim().equals("")) {
					throw new EndExcelException();
				}
				if(curRow == 0) {
					return;
				}
				List<String> values = new ArrayList<>();
				int position = 0;
				for(int i = 0; i < rowList.size(); i++) {
					String s = rowList.get(i).trim();
					if(!s.equals("")) {
						if(i >= 2) {
							// 2是小学，3是初中，4是高中
							position = i;
							values.add(s);
						}
						else {
							values.add(s);
						}
					}
				}
				if(values.size() == 3) {
					String[] kpoints = values.get(2).split(";");
					String value = "";
					for(String kpoint: kpoints) {
						if(!kpoint.trim().equals("")) {
							value += position+"_"+kpoint.trim()+";";
						}
					}
					kpointChangeMap.put(values.get(0)+"_"+values.get(1), value.substring(0, value.length() - 1));
				}
			}
			
			@Override
			public void onExcelEnd() {
				
			}
		});
		try {
			excelUtil.process(AFanTi.class.getResourceAsStream(path));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	private static List<KnowledgePoint> getKpoints(long id, String name){
		KnowledgePointMapper pointMapper = SpringBeanUtil.getBean(KnowledgePointMapper.class);
		if(kpointMap == null) {
			synchronized (synObject) {
				if(kpointMap == null) {
					List<Map<String, Object>> allList = pointMapper.findAll();
					kpointMap = new HashMap<>();
					for(Map<String, Object> map: allList) {
						KnowledgePoint knowledgePoint = new KnowledgePoint();
						knowledgePoint.setId((int)map.get("ID"));
						knowledgePoint.setName((String)map.get("NAME"));
						knowledgePoint.setPhaseId((int)map.get("PHASEID"));
						kpointMap.put(knowledgePoint.getPhaseId() + "_"+knowledgePoint.getName(), knowledgePoint);
						kpointMap.put(knowledgePoint.getName(), knowledgePoint);
					}
				}
			}
		}
		String key = kpointChangeMap.get(id+"_"+name);
		List<KnowledgePoint> returnList = new ArrayList<>();
		if(key != null) {
			String[] kpointKeys = key.split(";");
			for(String kpointKey: kpointKeys) {
				if(!kpointKey.equals("")) {
					KnowledgePoint knowledgePoint = kpointMap.get(kpointKey);
					if(knowledgePoint == null) {
						knowledgePoint = kpointMap.get(kpointKey.split("_")[1]);
					}
					if(knowledgePoint != null) {
						returnList.add(knowledgePoint);
					}
				}
			}
			
		}
		return returnList;
	}

	public float getSearchScore() {
		return searchScore;
	}

	public void setSearchScore(float searchScore) {
		this.searchScore = searchScore;
	}
}
